<?php
    /**
    * 负责解析uwa widget的xhtml文件，并解析 title ,icon, meta 和preference 信息， 返回json结果
    * 结果格式参考下面格式 
    * <code>
{
  "title":"My widget title",
  "icon":
  {
  "0":"http:\/\/www.netvibes.com\/favicon.ico"},
  "metas":
           {
           "author":"The author's name",
           "description":"The description",
           "apiVersion":"1.0",
           "autoRefresh":"1.0",
           "debugMode":"true"
           }
  ,
  "preferences":
           [
                   {
                   "name":"list",
                   "type":"list",
                   "label":"Testing list",
                   "onchange":"refresh",
                   "defaultValue":"1st",
                   "options":
                           [
                                   {
                                   "value":"all",
                                   "label":"all"
                                   }
                           ,
                                   {
                                   "value":"1st",
                                   "label":"First category"
                                   }
                           ,
                                   {
                                   "value":"2nd",
                                   "label":"Second category"
                                   }
                           ,
                                   {
                                   "value":"3rd",
                                   "label":"Third category"
                                   }
                           ]
                   }
           ]
  }

    * </code>
    * @author rainx
    * @param $widget_content 代表 widget 内容的 xhtml 代码
    */

function ynuwa_get_preference_from_widget(/*string*/ $widget_content)
{
    if (empty($widget_content))
    {
        return false;
    }
    
    $ret_arr = array();

    // 全局替换 widget:preferences 为 preferences 便于解析
    // TODO : note 这里面有可能会出现 widget:preference字符串被错误替换的问题，但是几率太低了,暂时忽略，
    // 但应该是潜在问题的点
    $widget_content = str_replace("widget:preferences", "widgetpreferences", $widget_content);

	//HACK: 去掉<script> 和 <style> 标签防止一些非法字符

	$widget_content = preg_replace("%(<script.*?>)(.*?)(<\/script>)%is", "",$widget_content);
	$widget_content = preg_replace("%(<style.*?>)(.*?)(<\/style>)%is", "",$widget_content);
	
    // 解析 meta
    $xml= new SimpleXMLElement($widget_content); 
    $meta_data = array();
    foreach ($xml->head->meta as $data) {
        $index= (string) $data['name'];
        $value= (string) $data['content'];
        $meta_data[$index]= $value;
    }

    // 解析 title

    if (isset ($xml->head->title[0]))
    {
        $title = (string) $xml->head->title[0];
    }

    // 解析icon
    $icon = array();
    // Additional data
    foreach ($xml->head->link as $data) {
        $index= (string) $data['rel'];
        $value= (string) $data['href'];
        if ($index == "icon")
            $icon[]= $value;
    }
    

    // 解析 Preference settings

    $preference_settings = array();

    // Preference settings
    if (!isset ($xml->head->widgetpreferences->preference)) {
        $xml->head->widgetpreferences->preference= array();
    }

    foreach ($xml->head->widgetpreferences->preference as $preference) {
        $index= (string) $preference['name'];
        
        $row = array(); 

        $row['name'] = $index;

        if (isset ($preference['label']))
        $row['label']= (string) $preference['label'];
        if (isset ($preference['type']))
        $row['type']= (string) $preference['type'];
        if (isset ($preference['defaultValue']))
        $row['defaultValue']= (string) $preference['defaultValue'];
        if (isset ($preference['onchange']))
        $row['onchange']= (string) $preference['onchange'];
        if (isset ($preference['min']))
        $row['min']= (string) $preference['min'];
        if (isset ($preference['max']))
        $row['max']= (string) $preference['max'];
        if (isset ($preference['step']))
        $row['step']= (string) $preference['step'];
        if (isset ($preference->option)) {
            $options = array();
            foreach ($preference->option as $option) {
                $label= (string) $option['label'];
                $value= (string) $option['value'];
                $options[]= array("label" => $label, "value" => $value);
            }
            $row['options'] = $options;
        }
        $preference_settings[] = $row;
    }


    $ret_arr = array(
        "title" => $title,
        "metas" => $meta_data,
        "icon" => $icon,
        "preferences" => $preference_settings
    );     
    
    return $ret_arr;
}

/**
$content = file_get_contents("http://biz.cn.yahoo.com/finance_widget.html");
var_dump(ynuwa_get_preference_from_widget($content));
**/

?>
